#pragma once

#include <iostream>
#include <string>
#include <vector>
#include <ctemplate/template.h>


#include "oj_model.hpp"

const std::string template_path = "./template_html/";
namespace ns_view
{
    using namespace ns_model;
    class View
    {
    public:
        View() {}
        ~View() {}
    public:
        void expandallhtml(const std::vector<Question> &qs, std::string *html)
        {
            // 1.形成路径
            std::string src_html = template_path + "template_all.html";
            
            // 2. 形成数据字典
            ctemplate::TemplateDictionary root("all_questions");
            for (const auto &q : qs)
            {
                ctemplate::TemplateDictionary *sub = root.AddSectionDictionary("question_list");
                sub->SetValue("number", q.number);
                sub->SetValue("title", q.title);
                sub->SetValue("grade", q.grade);
            }

            // 3.获取被渲染的html
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);

            // 4.开始完成渲染功能
            tpl->Expand(html, &root);

        }
        bool expandonehtml(const Question &q, std::string *html)
        {
            // 1.形成路径
            std::string src_html = template_path + "template_one.html";
            
            // 2. 形成数据字典
            ctemplate::TemplateDictionary root("one_questions");
            root.SetValue("number", q.number);
            root.SetValue("title", q.title);
            root.SetValue("grade", q.grade);
            root.SetValue("desc", q.desc);
            root.SetValue("pre_code", q.head);

            // 3.获取被渲染的html
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);

            // 4.开始完成渲染功能
            tpl->Expand(html, &root);

            return true;
        }
    };
}